package com.sbtr.util;

import java.io.*;
import java.util.Enumeration;
import java.util.List;
//import java.util.zip.ZipEntry;
//import java.util.zip.ZipFile;
//import java.util.zip.ZipOutputStream;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * zip压缩文件处理工具类
 * 
 * @author oujh5
 *
 */
public class ZipUtil {

	private ZipUtil() {}
	private static Logger logger = LoggerFactory.getLogger(ZipUtil.class);

	/**
	 * 解压文件
	 *
	 * @param zipFile   待解压文件
	 * @param targetDir 解压目标路径，绝对路径，文件夹
	 * @return
	 */
	public static boolean unZip(File zipFile, String targetDir) {
		byte[] buff = new byte[1024];
		try {
			ZipFile zip = new ZipFile(zipFile, "GBK");
			// 创建目标目录
			File targetFile = new File(targetDir);
			if (!targetFile.exists() || targetFile.isFile()) {
				targetFile.mkdirs();
			}
			// 返回zip包中所有的项，包括包中的文件夹以及文件夹中的文件或子文件夹
			Enumeration<?> entries = zip.getEntries();
			while (entries.hasMoreElements()) {
				ZipEntry entry = (ZipEntry) entries.nextElement();
				File entryFile = new File(targetFile.getAbsoluteFile() + File.separator + entry.getName());
				if (entry.isDirectory()) { // 若是文件夹则创建文件夹
					entryFile.mkdir();
				} else { // 若是目录则创建目录，并copy entry内容
					if (!entryFile.getParentFile().exists()) {
						entryFile.getParentFile().mkdirs();
					}
					if (entryFile.createNewFile()) {
						try (FileOutputStream fos = new FileOutputStream(entryFile);
								InputStream in = zip.getInputStream(entry)) {
							int len = -1;
							while ((len = in.read(buff)) > 0) {
								fos.write(buff, 0, len);
							}
						}
					}
				}
			}
			zip.close();
		} catch (Exception e) {
			logger.error("解压文件失败", e);
		}
		return false;
	}

	public static boolean zipFile(String zipFileDir, List<File> srcFiles) {
		byte[] buffer = new byte[1024];
		try (FileOutputStream fos = new FileOutputStream(new File(zipFileDir));
				ZipOutputStream zout = new ZipOutputStream(fos);) {
			// 压缩流
			for (File f : srcFiles) {
				if (f.exists() && f.isFile()) {
					// 文件输入流
					try (FileInputStream fis = new FileInputStream(f);) {
						// 压缩条目
						ZipEntry entry = new ZipEntry(f.getName());
						// 放入下一个条目
						zout.putNextEntry(entry);
						int len = -1;
						while ((len = fis.read(buffer)) != -1) {
							zout.write(buffer, 0, len);
						}
						// 关闭条目
						zout.closeEntry();
					}
				}
			}
			return true;
		} catch (IOException e) {
			logger.error("文件压缩失败", e);
		}
		return false;
	}


	public static void zipFileWithTier(String srcFiles, String zipPath) {
		try {

			FileOutputStream zipFile = new FileOutputStream(zipPath);
			BufferedOutputStream buffer = new BufferedOutputStream(zipFile);
			ZipOutputStream out = new ZipOutputStream(buffer);
			zipFiles(srcFiles, out, "");
			out.close();
			buffer.close();
			zipFile.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			logger.error("文件压缩失败", e);
		}
	}


	public static void zipFiles(String filePath, ZipOutputStream out, String prefix)
			throws IOException {
		File file = new File(filePath);
		if (file.isDirectory()) {
			if (file.listFiles().length == 0) {
				ZipEntry zipEntry = new ZipEntry(prefix + file.getName() + "/");
				out.putNextEntry(zipEntry);
				out.closeEntry();
			} else {
				prefix += file.getName() + File.separator;
				for (File f : file.listFiles())
					zipFiles(f.getAbsolutePath(), out, prefix);
			}
		} else {
			FileInputStream in = new FileInputStream(file);
			ZipEntry zipEntry = new ZipEntry(prefix + file.getName());
			out.putNextEntry(zipEntry);
			byte[] buf = new byte[1024];
			int len;
			while ((len = in.read(buf)) > 0) {
				out.write(buf, 0, len);
			}
			out.closeEntry();
			in.close();
		}

	}


}
